package com.dqhl.business.adapter;

import android.content.Context;
import android.icu.util.ValueIterator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.Coupon;
import com.dqhl.business.util.ViewHolder;

import java.util.List;

/**
 * Created by guanluocang on 2016/11/7.
 */
public class CouponsAdapter extends BaseAdapter {
    private Context context;
    private List<Coupon> couponList;
    private LayoutInflater inflater;
    private OnOperationListener onOperationListener;

    public CouponsAdapter(Context context, List<Coupon> couponList, OnOperationListener onOperationListener) {
        this.context = context;
        this.couponList = couponList;
        inflater = LayoutInflater.from(context);
        this.onOperationListener = onOperationListener;
    }

    @Override
    public int getCount() {
        return couponList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.layout_coupon_item, viewGroup, false);
        }

        TextView tv_condition = ViewHolder.get(view, R.id.tv_condition);
        TextView tv_conponContent = ViewHolder.get(view, R.id.tv_conponContent);

        TextView tv_deleteCoupon = ViewHolder.get(view, R.id.tv_deleteCoupon);
        TextView tv_updateCoupon = ViewHolder.get(view, R.id.tv_updataCoupon);
        TextView tv_release = ViewHolder.get(view, R.id.tv_release);

        Coupon coupon = couponList.get(i);

        tv_condition.setText(coupon.getCondition());
        tv_conponContent.setText(coupon.getDenomination());


        tv_deleteCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onOperationListener.deleteCoupon(i);
            }
        });
        tv_updateCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onOperationListener.modifyCoupon(i);
            }
        });
        tv_release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onOperationListener.releaseCoupon(i);
            }
        });

        return view;
    }

    public interface OnOperationListener {
        void modifyCoupon(int position);

        void deleteCoupon(int position);

        void releaseCoupon(int position);
    }
}
